//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.siecom.tools;

import android.util.Log;

public class ByteUtil {
    private static final String TAG = ByteUtil.class.getSimpleName();

    public ByteUtil() {
    }

    public static byte[] parseByteArray(String hexString) {
        if(hexString == null) {
            return null;
        } else if(hexString.length() % 2 == 1) {
            throw new RuntimeException("hexString长度为奇数");
        } else {
            byte[] bytes = new byte[hexString.length() / 2];
            String str = "0123456789ABCDEF";

            for(int i = 0; i < hexString.length(); i += 2) {
                int indexOfHigh = str.indexOf(Character.toUpperCase(hexString.charAt(i)));
                int indexOfLow = str.indexOf(Character.toUpperCase(hexString.charAt(i + 1)));
                if(indexOfHigh == -1 || indexOfLow == -1) {
                    Log.e(TAG, "hexString = " + hexString + ", index = " + i);
                    throw new RuntimeException("hexString转byte[]时, 发现hexString中有异常字符");
                }

                bytes[i / 2] = (byte)(indexOfHigh << 4 | indexOfLow);
            }

            return bytes;
        }
    }

    public static String toHexString(byte b) {
        return toHexString(new byte[]{b});
    }

    public static String toHexString(byte[] bytes) {
        return toHexString(bytes, 0, bytes.length);
    }

    public static String toHexString(byte[] bytes, int len) {
        return toHexString(bytes, 0, len);
    }

    public static String toHexString(byte[] bytes, int offset, int len) {
        StringBuilder builder = new StringBuilder(bytes.length * 2);
        String str = "0123456789ABCDEF";
        int limitIndex = offset + len;
        if(limitIndex > bytes.length) {
            limitIndex = bytes.length;
        }

        for(int i = offset; i < limitIndex; ++i) {
            builder.append(str.charAt(bytes[i] >>> 4 & 15));
            builder.append(str.charAt(bytes[i] & 15));
        }

        return builder.toString();
    }

    public static byte[] removeTail(byte[] byteArray, byte b) {
        int i;
        for(i = byteArray.length - 1; i >= 0 && byteArray[i] == b; --i) {
            ;
        }

        return arrayCopy(byteArray, i + 1);
    }

    public static byte[] rigthShiftArray(byte[] b, int leftShiftByteNumber) {
        return leftShiftArray(b, leftShiftByteNumber * -1);
    }

    public static byte[] leftShiftArray(byte[] b, int leftShiftByteNumber) {
        byte[] result = new byte[b.length];
        int absLeftShiftByteNumber = Math.abs(leftShiftByteNumber);
        if(absLeftShiftByteNumber >= b.length) {
            return result;
        } else {
            if(leftShiftByteNumber >= 0) {
                System.arraycopy(b, leftShiftByteNumber, result, 0, b.length - absLeftShiftByteNumber);
            } else {
                System.arraycopy(b, 0, result, absLeftShiftByteNumber, b.length - absLeftShiftByteNumber);
            }

            return result;
        }
    }

    public static int byte2Int(byte[] b) {
        int intValue = 0;
        int i = b.length - 1;

        for(int j = 0; i >= 0 && j < 4; ++j) {
            intValue += (b[i] & 255) << 8 * j;
            --i;
        }

        return intValue;
    }

    public static byte[] arrayCopy(byte[] src, int len) {
        return arrayCopy(src, 0, len);
    }

    public static byte[] arrayCopy(byte[] src, int srcOffset, int len) {
        byte[] dst = new byte[len];
        System.arraycopy(src, srcOffset, dst, 0, len);
        return dst;
    }

    public static byte[] subBytes(byte[] src, int start, int end) {
        return arrayCopy(src, start, end - start);
    }

    public static byte[] subBytes(byte[] src, int start) {
        return subBytes(src, start, src.length);
    }

    public static String arrayCopy2Hex(byte[] src, int srcOffset, int len) {
        byte[] subByte = arrayCopy(src, srcOffset, len);
        return toHexString(subByte);
    }

    public static boolean notByteArray(Object obj) {
        return obj == null?true:obj.getClass().getComponentType() != Byte.TYPE;
    }

    public static boolean isByteArray(Object obj) {
        return obj == null?false:obj.getClass().getComponentType() == Byte.TYPE;
    }

    public static byte[] concat(byte[] b1, byte[] b2) {
        if(b1 == null) {
            return b2;
        } else if(b2 == null) {
            return b1;
        } else {
            byte[] result = new byte[b1.length + b2.length];
            System.arraycopy(b1, 0, result, 0, b1.length);
            System.arraycopy(b2, 0, result, b1.length, b2.length);
            return result;
        }
    }

    public static byte[] concat(byte[]... bytesArray) {
        byte[] reulstBytes = new byte[0];

        for(int i = 0; i < bytesArray.length; ++i) {
            reulstBytes = concat(reulstBytes, bytesArray[i]);
        }

        return reulstBytes;
    }

    public static boolean is2DByteArray(Object obj) {
        return obj == null?false:obj.getClass().getComponentType() == byte[].class;
    }

    public static int indexOf(byte[] bytes, byte b) {
        for(int i = 0; i < bytes.length; ++i) {
            if(bytes[i] == b) {
                return i;
            }
        }

        return -1;
    }
}
